import {UserModel} from "../../../models/user"
import WxValidate from '../../../utils/WxValidate'
import {config} from "../../../config";
const app = getApp()
const userModel = new UserModel()
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        index: null,
        picker: ['女', '男'],
        identityIndex: null,
        identityPicker: ['摄影师', '模特'],
        date: null,
        region: null,
        defaultRegion: ['北京市', '北京市', '东城区'],
        avatarUrl: ''
    },
    PickerChange(e) {
        console.log(e);
        const userInfo = this.data.userInfo
        userInfo.gender = e.detail.value
        this.setData({
            userInfo
        })
    },
    identityPickerChange(e) {
        console.log(e);
        const userInfo = this.data.userInfo
        userInfo.capacity = Number(e.detail.value) +1
        this.setData({
            userInfo
        })
    },
    DateChange(e) {
        const userInfo = this.data.userInfo
        userInfo.birthday = e.detail.value
        this.setData({
            userInfo
        })
    },
    RegionChange: function (e) {
        const userInfo = this.data.userInfo
        userInfo.region = e.detail.value
        this.setData({
            userInfo
        })
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../center/index'
        })
    },

    ChooseImage() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], //从相册选择
            success: (res) => {
                this.uploadUrl(res.tempFilePaths[0])
            }
        });
    },

    _Load() {
        userModel.getUserInfo().then(res => {
            this.setData({
                userInfo: res.data
            })
        })
    },
    uploadUrl(path) {
        let _this = this
        wx.uploadFile({
            url: config.api_blink_url+'/v/user/updateUserAvatar',
            filePath: path,
            header:{
                'token': wx.getStorageSync('token')
            },
            name: 'file',
            success (res){
                console.log(res.data)
                if (res.statusCode ==200) {
                    const userInfo = _this.data.userInfo
                    userInfo.avatarUrl = path
                    _this.setData({
                        userInfo
                    })
                    app.showInfo('修改成功！',app.SUCCESS)
                }
            }
        })
    },

    formSubmit(e) {
        // 校验表单
        const params = e.detail.value
        params.gender = this.data.userInfo.gender
        params.birthday = this.data.userInfo.birthday
        params.region = this.data.userInfo.region
        params.capacity = this.data.userInfo.capacity
        if (!this.WxValidate.checkForm(params)){
            const error = this.WxValidate.errorList[0];
            app.showInfo(error.msg)
            return false
        }
        console.log(params)
        this.submitInfo(params)
    },

    submitInfo(params) {
        let _this = this
        const userInfo = this.data.userInfo
        params.userId = userInfo.userId
        params.region = userInfo.region.join(' ')
        params.province = userInfo.region[1]
        params.capacity = userInfo.capacity

        userModel.updateUser(params).then(res=>{
          app.showInfo('修改成功！',app.SUCCESS)
          wx.navigateBack({
            delta: 1
          })
        })
    },

    onLoad: function () {
        this.initValidate()
        this._Load()
    },
    // 初始化表单校验
    initValidate(){
        const rules = {
            gender: {
                required: true
            },
            region: {
                required: true
            },
            birthday: {
                required: true
            },
            capacity: {
                required: true
            },
            phone: {
                required: true,
                tel: true,
            },
            weChat: {
                required: true,
                rangelength: [1,100]
            },
            weibo: {
                rangelength: [1,100]
            },
        }
        const messages = {
            gender: {
                required: '请选择性别'
            },
            birthday: {
                required: '请选择生日'
            },
            region: {
                required: '请选择地区'
            },
            capacity: {
                required: '请选择身份类型'
            },
            phone: {
                required: '请输入11位手机号码',
                tel: '请输入正确的手机号码',
            },
            weChat: {
                required: '请输入微信号码',
                rangelength:'微信号码为2~30个字符',
            },
            weibo:{
                rangelength:'微博号为2~50个字符'
            },
        }
        this.WxValidate = new WxValidate(rules, messages)
    },
})
